package com.atguigu.spzx.manager.service.Impl;

import com.atguigu.spzx.manager.mapper.CategoryBrandMapper;
import com.atguigu.spzx.manager.service.CategoryBrandService;
import com.atguigu.spzx.model.dto.product.CategoryBrandDto;
import com.atguigu.spzx.model.entity.product.Brand;
import com.atguigu.spzx.model.entity.product.CategoryBrand;
import com.atguigu.spzx.service.exception.GuliException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * ClassName: CategoryBrandServiceImpl
 * Package: com.atguigu.spzx.manager.service.Impl
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/5 16:53
 * @Version 1.0
 */
@Service
public class CategoryBrandServiceImpl implements CategoryBrandService {
    @Resource
    private CategoryBrandMapper categoryBrandMapper;



    /**
     * 分页带条件查询分类品牌
     * @param categoryBrandDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<CategoryBrand> findByPage(CategoryBrandDto categoryBrandDto, Long pageNum, Long pageSize) {
        PageHelper.startPage(pageNum.intValue(),pageSize.intValue());
        List<CategoryBrand> list =  categoryBrandMapper.findByPage(categoryBrandDto);
        PageInfo<CategoryBrand> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public void save(CategoryBrand categoryBrand) {
        if(categoryBrand.getCategoryId() == null || categoryBrand.getBrandId()==null){
            throw new GuliException(201,"请选择完整的分类和品牌");
        }
        // 添加分类品牌时要注意分类id和品牌id的复合索引
        assertCategoryIdBrandIdUnique(categoryBrand);
        categoryBrandMapper.save(categoryBrand);
    }

    /**
     * 添加分类品牌时要注意分类id和品牌id的复合索引
     * @param categoryBrand
     */
    private void assertCategoryIdBrandIdUnique(CategoryBrand categoryBrand) {
        CategoryBrandDto categoryBrandDto = new CategoryBrandDto();
        categoryBrandDto.setCategoryId(categoryBrand.getCategoryId());
        categoryBrandDto.setBrandId(categoryBrand.getBrandId());
        List<CategoryBrand> categoryBrandList = categoryBrandMapper.findByPage(categoryBrandDto);
        if(!CollectionUtils.isEmpty(categoryBrandList)){
            throw new GuliException(209,"该分类和品牌关系已经存在！");
        }
    }

    /**
     * 根据id修改
     * @param categoryBrand
     */
    @Override
    public void updateById(CategoryBrand categoryBrand) {
        // 修改分类品牌时要注意分类id和品牌id的复合索引
        assertCategoryIdBrandIdUnique(categoryBrand);
        categoryBrandMapper.updateById(categoryBrand);

    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        categoryBrandMapper.deleteById(id);
    }

    /**
     * 根据分类id查询品牌
     * @param id
     * @return
     */
    @Override
    public List<Brand> findBrandByCategoryId(Long id) {
        return categoryBrandMapper.findBrandByCategoryId(id);
    }
}
